<?php
/**
 * 
 * User 类
 * @author will
 *
 */
class User {
	private static $PASSWORD_KEY = '@!phzzy~*)';
	
	/**
	 * 加密密码
	 * @param unknown_type $password
	 */
	public static function encodePassword($password) {
		return md5($password . self::$PASSWORD_KEY);
	}
	
	/**
	 * 检查密码是否正确
	 * @param unknown_type $userName
	 * @param unknown_type $password
	 * @param unknown_type $remember
	 */
	public static function checkPassword($mobile , $password) {
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `user_baseinfo` WHERE `mobile` = '{$mobile}'");
		if(empty($result)) {
			return false;
		} else {
			if( $result['password'] != self::encodePassword($password) ) {
				return false;
			} else {
				return array(
					'uin' => $result['uin'],
					'mobile' => $result['mobile'],
					'name' => $result['name'],
				);
			}
		}
	}
	
	/**
	 * 根据手机号获取用户信息
	 * Enter description here ...
	 * @param unknown_type $mobile
	 */
	public static function getByMobile($mobile) {
		$Db = loadDB();
		
		$result = $Db->getOne("SELECT `uin` , `mobile` , `name` , `sex` ,`proving` FROM `user_baseinfo` WHERE `mobile` = {$mobile}");
		
		return empty($result) ? array() : $result;
	}
	
	/**
	 * 获取用户基础数据
	 * 	字段：	mobile name sex
	 * @param <array | int> $uins
	 */
	public static function getBaseInfo($uins) {
		if(empty($uins)) return array();
		
		$Db = loadDB();
		if(is_array($uins)) {
			$uins = array_unique($uins);
			$uins_str = implode(',' , $uins);
			$result = $Db->getAll("SELECT `uin` , `mobile` , `name` , `sex` , `proving` , `logo_state` FROM `user_baseinfo` WHERE `uin` IN ({$uins_str})" , 'uin');
		} else {
			$result = $Db->getOne("SELECT `uin` , `mobile` , `name` , `sex` , `proving` , `logo_state` FROM `user_baseinfo` WHERE `uin` = {$uins}");
		}
		
		return empty($result) ? array() : $result;
	}
	
	/**
	 * 获取用户密码
	 * 	字段：	password
	 * @param <int> $uin
	 */
	public static function getPassword($uin) {
		$Db = loadDB();
		$result = $Db->getOne("SELECT `password` FROM `user_baseinfo` WHERE `uin` = ({$uin})");
		
		return empty($result) ? null : $result['password'];
	}
	
	/**
	 * 获取用户的全部数据
	 * 	字段：	mobile name sex birth city homecity intro reg_time login_time
	 * @param <array | int> $uins
	 */
	public static function getFullInfo($uins) {
		if(empty($uins)) return array();
		
		$Db = loadDB();
		if(is_array($uins)) {
			$uins = array_unique($uins);
			$uins_str = implode(',' , $uins);
			$result = $Db->getAll("SELECT `mobile` , `name` , `sex` , `proving` , `logo_state` , `ue`.* 
																	FROM `user_baseinfo` as `ub` 
																		LEFT JOIN `user_extrainfo` as `ue` 
																		ON `ue`.`uin` = `ub`.`uin` 
																	WHERE `ub`.`uin` IN ({$uins_str})" , 'uin');
		} else {
			$result = $Db->getOne("SELECT `mobile` , `name` , `sex` , `proving` , `logo_state` ,`ue`.* 
																	FROM `user_baseinfo` as `ub` 
																		LEFT JOIN `user_extrainfo` as `ue` 
																		ON `ue`.`uin` = `ub`.`uin` 
																	WHERE `ub`.`uin` = ({$uins})");
		}
		
		return empty($result) ? array() : $result;
	}
	
	/**
	 * 修改用户资料
	 * @param <int> $uin
	 * @param <array> $info
	 */
	public static function updateInfo($uin , $info) {
		if(empty($info)) return 0;
		
		// 区分是要更新哪个表
		$baseinfo_key  = array('name' , 'password' , 'sex');
		$extrainfo_key = array('birth' , 'city' , 'homecity' , 'intro' , 'reg_time' , 'login_time');
		
		$baseinfo_array = array();
		$extrainfo_array = array();
		foreach($baseinfo_key as $key) {
			if(isset($info[$key])) {
				if($key == 'password') {
					$baseinfo_array[$key] = self::encodePassword($info[$key]);
				} else {
					$baseinfo_array[$key] = $info[$key];
				}
			}
		}
		
		foreach($extrainfo_key as $key) {
			if(isset($info[$key])) {
				$extrainfo_array[$key] = $info[$key];
			}
		}
		
		// 更新基本资料
		$success = 0;
		if(!empty($baseinfo_array)) {
			$success = self::updateBaseInfo($uin, $baseinfo_array);
		}
		
		if($success == 0 && !empty($extrainfo_array)) {
			$success = self::updateExtraInfo($uin, $extrainfo_array);
		}
		
		return $success;
	}
	
	/**
	 * 修改基本资料
	 * @param <int> $uin
	 * @param <array> $info
	 */
	public static function updateBaseInfo($uin , $info) {
		if(empty($info)) return 0;
		
		$Db = loadDB();
		$success = $Db->doUpdate('user_baseinfo' , $info , "`uin` = {$uin}");
		
		return $success ? 0 : 302;
	}
	
	
	/**
	 * 修改其他资料
	 * @param <int> $uin
	 * @param <array> $info
	 */
	public static function updateExtraInfo($uin , $info) {
		if(empty($info)) return 0;
		
		$Db = loadDB();
		$success = $Db->doUpdate('user_extrainfo' , $info , "`uin` = {$uin}");
		
		return $success ? 0 : 303;
	}
	
	/**
	 * 获取图片地址
	 * Enter description here ...
	 * @param <object> $user_info
	 * @param <int> $type
	 */
	public static function getLogo($user_info , $type = 30) {
		$uin = $user_info['uin'];
		$img_url = '';
		if($user_info['logo_state'] < 2) {
			if($user_info['sex'] == 2) {
				$img_url = SITEURL . '/image/user_logo/woman_' . $type . '.jpg';
			} else {
				$img_url = SITEURL . '/image/user_logo/man_' . $type . '.jpg';
			}
		} else {
			$path = floor($uin / 500);
			$path = self::getPath($uin);
			$img_url = SITEURL . $path . $uin .'_' . $type . '.jpg';
		}
		//print_r($img_url);
		return $img_url;
	}
	
	/**
	 * 图片文件夹路径
	 * @param <int> $uin 
	 */
	public static function getPath($uin){
		$path = floor($uin / 500);
		$path = '/image/user_logo/' . $path .'/';
		
		return $path;
	}
}

# end of script
